const router = require('koa-router')()
const GroupHelp =require('../database/groups/GroupHelp')
const BlackListHelp =require('../database/blacklists/BlackListHelp')
const JpushsHelp =require('../database/jpush/JpushsHelp')
const VerifyUtils=require('../utils/VerifyUtils')



router.get('/api/hrj/getGrouplist', async (ctx, next) => {

    let myuid=ctx.request.query.myuid-0
    VerifyUtils.tips(ctx,{myuid})

    let groups=await GroupHelp.getGroupList(myuid)

    let groups_=[]
    groups.map((item)=>{
        let obj={
            groupid:item.gid+"",
            groupname:item.gname,
            ownflag: ""
        }
        groups_.push(obj)
    })

    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: "",
        groups:groups_,
        ismoderator:null,
        member_avatar:'',
        member_uid:'',
        member_username:'',
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: "",
        status: "1"
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})





router.get('/api/hrj/getGroupInfo', async (ctx, next) => {

    let groupid=ctx.request.query.groupid-0
    VerifyUtils.tips(ctx,{groupid})

    let ginfo=await GroupHelp.getGroupInfo(groupid)

    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        group:{
            avatar:"",
            groupid: ginfo.gid+"",
            groupname: ginfo.gname,
            userlist:JSON.parse(ginfo.userlist)
        },
        groupid: ginfo.gid+"",
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: ""
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})



//查询群成员
router.get('/api/hrj/queryGroupMembers', async (ctx, next) => {

    let groupid=ctx.request.query.groupid-0
    VerifyUtils.tips(ctx,{groupid})
    let ginfo=await GroupHelp.getGroupInfo(groupid)
    let userlist=JSON.parse(ginfo.userlist)

    let users_=[]
    userlist.map((item)=>{
        let obj={
            uid:item.uid+"",
            username:item.username,
            avatar:item.pic,
        }
        users_.push(obj)
    })


    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: ginfo.gid+"",
        groupname: ginfo.gname,
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        ownuid:ginfo.owner_uid+'',
        readaccess:"0",
        saltkey: "",
        status: "1",
        users:users_,
        total_user_num:200,
        share_url:`https://www.huarenjie.com/?groupid=${groupid}`
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})


//创建群组
router.get('/api/hrj/createGroup', async (ctx, next) => {

    let myuid=ctx.request.query.myuid-0
    let groupname=ctx.request.query.groupname
    VerifyUtils.tips(ctx,{groupname},{myuid})
    let ginfo=await GroupHelp.hrjCreateGroup(myuid,groupname)

    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: ginfo.gid+'',
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: "",
        status: "1"
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})




//修改群名
router.get('/api/hrj/changeGroupName', async (ctx, next) => {

    let myuid=ctx.request.query.myuid-0
    let groupid=ctx.request.query.groupid-0
    let groupname=ctx.request.query.groupname

    VerifyUtils.tips(ctx,{groupid},{groupname},{myuid})

    let ginfo=await GroupHelp.changeGroupName(groupid,groupname)
    let groups=await GroupHelp.getGroupList(myuid)
    let groups_=[]
    groups.map((item)=>{
        let obj={
            groupid:item.gid+"",
            groupname:item.gname,
        }
        groups_.push(obj)
    })


    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: ginfo.gid+'',
        groups:groups_,
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: "",
        status: "1"
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})




//加入群组，添加用户
router.get('/api/hrj/joinGroup', async (ctx, next) => {

    let groupid=ctx.request.query.groupid-0
    let uids=ctx.request.query.userid  //字符串


    VerifyUtils.tips(ctx,{groupid},{uids})

    let res=await GroupHelp.hrjAddGroupUser(groupid,uids)

    let status="1"
    if(res.code==-1){
        status="-1"
    }
    let msg=res.msg

    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: groupid+"",
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: "",
        status: status,
        msg:msg
    }

    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data,
    }
})




//删除用户
router.get('/api/hrj/removeGroupUser', async (ctx, next) => {
    let groupid=ctx.request.query.groupid-0
    let userid=ctx.request.query.userid     //字符串
    let myuid=ctx.request.query.myuid-0
    VerifyUtils.tips(ctx,{groupid},{userid},{myuid})

    let res=await GroupHelp.hrjRemoveGroupUser(groupid,userid,myuid)

    if(res.code==-1){
        ctx.body=res

    }else{
        ctx.body={
            code:0,
            data:''
        }
    }

})





//退出群组
router.get('/api/hrj/exitGroup',async(ctx,next)=>{

    let groupid=ctx.request.query.groupid-0
    let myuid=ctx.request.query.myuid
    VerifyUtils.tips(ctx,{groupid},{myuid})

    await GroupHelp.exitGroup(groupid,myuid)


    let data={
        auth:"",
        cookiepre:"",
        formhash: "",
        groupid: groupid+"",
        ismoderator: null,
        member_avatar: "",
        member_uid: "",
        member_username: "",
        notice:{
            newmypost: "0",
            newpm: "0",
            newprompt: "0",
            newpush: "0",
        },
        readaccess:"0",
        saltkey: "",
        status: "1"
    }


    ctx.body={
        Version:"240",
        Charset: "UTF-8",
        Variables:data
    }
})



// 解散群组
router.get('/api/hrj/dismissGroup',async(ctx,next)=>{

    let groupid=ctx.request.query.groupid-0
    let myuid=ctx.request.query.myuid-0

    VerifyUtils.tips(ctx,{groupid},{myuid})

    let res=await GroupHelp.hrjDeleteGroup(groupid,myuid)
    if(res.code==11){
        ctx.body=res
    }
    else{
        let data={
            auth:"",
            cookiepre:"",
            formhash: "",
            groupid: groupid+"",
            ismoderator: null,
            member_avatar: "",
            member_uid: "",
            member_username: "",
            notice:{
                newmypost: "0",
                newpm: "0",
                newprompt: "0",
                newpush: "0",
            },
            readaccess:"0",
            saltkey: "",
            status: "1"
        }


        ctx.body={
            Version:"240",
            Charset: "UTF-8",
            Variables:data
        }
    }

})





//黑名单功能
//添加黑名单

router.post('/api/hrj/addblack',async(ctx,next)=>{

    let {myuid,uid}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid},{uid})

    let data=await BlackListHelp.addBlack(myuid-0,uid-0)


    ctx.body={
        code:0,
        data:data
    }

})




//移除黑名单
router.post('/api/hrj/delblack',async(ctx,next)=>{
    let {myuid,uid}=ctx.request.body

    VerifyUtils.tips(ctx,{myuid},{uid})

    let data=await BlackListHelp.delBlack(myuid-0,uid-0)

    ctx.body={
        code:0,
        data:data
    }
})


//黑名单列表
router.post('/api/hrj/blacklist',async(ctx,next)=>{
    let {myuid}=ctx.request.body

    VerifyUtils.tips(ctx,{myuid})

    let data=await BlackListHelp.getList(myuid-0)

    ctx.body={
        code:0,
        data:data
    }
})


//用户是否存在黑名单中
router.post('/api/hrj/checkblack',async(ctx,next)=>{

    let {myuid,uid}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid},{uid})

    let data=await BlackListHelp.checkBlackAll(myuid-0,uid-0,)  //我是否再对方的黑名单里

    ctx.body={
        code:0,
        data:data
    }
})




//极光推送是否开启
router.post('/api/hrj/getJpushState',async(ctx,next)=>{

    let {myuid}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid})

    let data=await JpushsHelp.getJpushState(myuid-0)
    let is_on=1
    if(data){
        is_on=data.is_on
    }

    ctx.body={
        code:0,
        data:{
            uid:myuid-0,
            is_on:is_on
        }
    }
})


router.post('/api/hrj/setJpushState',async(ctx,next)=>{

    let {myuid,is_on}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid},{is_on})

    let data=await JpushsHelp.setJpushState(myuid-0,is_on-0)

    ctx.body={
        code:0,
        data:{
            uid:myuid-0,
            is_on:is_on-0
        }
    }
})



//对某个群是否开启极光推送
router.post('/api/hrj/getGroupJpush',async(ctx,next)=>{

    let {myuid,gid}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid},{gid})

    let group_is_on=await JpushsHelp.getGroupJpush(myuid-0,gid-0)

    ctx.body={
        code:0,
        data:{
            uid:myuid-0,
            gid:gid-0,
            group_is_on:group_is_on
        }
    }
})


router.post('/api/hrj/setGroupJpush',async(ctx,next)=>{

    let {myuid,gid,is_on}=ctx.request.body
    VerifyUtils.tips(ctx,{myuid},{gid},{is_on})

    let data=await JpushsHelp.setGroupJpush(myuid-0,gid-0,is_on-0)

    ctx.body={
        code:0,
        data:{
            uid:myuid-0,
            gid:gid-0,
            group_is_on:is_on-0
        }
    }
})





module.exports = router